package com.stephen.redisdemo.entity.vo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.stephen.redisdemo.entity.enums.ResultEnum;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;
import java.util.HashMap;

/**
 * @author: stephen
 * @create: 2020-02-06 12:21
 * @description: 通用返回结果
 */
@Getter
@Setter
@ToString
// @JsonInclude(JsonInclude.Include.NON_NULL)
public class CommonResponse<T> implements Serializable {

    private Integer code;

    private String msg;

    private T data;

    public static CommonResponse ok() {
        return new CommonResponse (ResultEnum.SUCCUSS);
    }

    public static<T> CommonResponse<T> ok(T data) {
        return new CommonResponse (ResultEnum.SUCCUSS,data);
    }

    public static CommonResponse error() {
        return new CommonResponse (ResultEnum.FAIL);
    }

    public static<T> CommonResponse<T> error(T data) {
        return new CommonResponse (ResultEnum.FAIL,data);
    }

    public CommonResponse data(T data) {
        this.data = data;
        return this;
    }

    public CommonResponse mapData(String key,Object value) {
        HashMap<String, Object> map = new HashMap<>();
        map.put(key,value);
        this.data((T) map);
        return this;
    }

    public CommonResponse(ResultEnum resultEnum) {
        this.code = resultEnum.getCode();
        this.msg = resultEnum.getMsg();
    }

    public CommonResponse(ResultEnum resultEnum,T data) {
        this.code = resultEnum.getCode();
        this.msg = resultEnum.getMsg();
        this.data = data;
    }

    public CommonResponse(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public CommonResponse(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    @JsonIgnore
    public Boolean isSuccessed() {
        return this.code.equals(ResultEnum.SUCCUSS.getCode());
    }

}
